#import <CoreFoundation/CoreFoundation.h>
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

#import "TouchApp.h"

@implementation TouchApp

- (void)applicationDidFinishLaunching:(NSNotification*)aNotification
{
  // window
  window = [[UIWindow alloc] initWithContentRect:CGRectMake(0, 0, 320, 480)];

  // draw view
  //drawView = [[MyDrawView alloc] initWithFrame:CGRectMake(0, 0, 320, 320)];
  drawView = [[MyDrawView alloc] initWithFrame:CGRectMake(160, 160, 0, 0)];
  [drawView setSuperApp:self];

  // text
  textView1 = [[UITextView alloc] initWithFrame:CGRectMake(0, 320 + 1, 320, 80 - 1)];
  [textView1 setTextFont:@"Courier"];
  [textView1 setTextSize:12];
  [textView1 setText:@"\n"
             @"\n"
             @"\n"
             @"\n"
             @"usage: double tap - log mode ON/OFF"];

  textView2 = [[UITextView alloc] initWithFrame:CGRectMake(0, 400 + 1, 320, 80 - 1)];
  [textView2 setTextFont:@"Helvetica"];
  [textView2 setTextSize:12];

  // main view
  mainView = [[UIView alloc] initWithFrame: [window bounds]];
  
  // set window
  [mainView addSubview:drawView];
  [mainView addSubview:textView1];
  [mainView addSubview:textView2];
  [textView1 autorelease];
  [textView2 autorelease];

  [window setContentView:mainView];
  [mainView autorelease];

  [window orderFront:self];
  [window makeKey:self];
  [window _setHidden:NO];

  // hide status bar (animation)
  [self setStatusBarMode:2 duration:1.0];

  // show main view (animation)
  [UIView beginAnimations:nil]; 
  [UIView setAnimationDuration:1.0]; 
  [drawView setFrame:CGRectMake(0, 0, 320, 320)];
  [UIView endAnimations];
}

- (void)applicationWillTerminate {
  [window release];
  [drawView release];
}

- (void)acceleratedInX:(float)x Y:(float)y Z:(float)z;
{
  [drawView setBgcolor:x+0.5:y+0.5:z+0.5:1];
  if (x > 0.49 || x < -0.49) {
    [drawView swapImage];
  }
  [drawView setNeedsDisplay];
}


- (void)toggleLogMode {
  [self appendStatus1:@"log mode: OFF"];
  logFlag = !logFlag;
  [self appendStatus1:@"log mode: ON"];
}

- (void)appendStatus1:(NSString *)status {
  static int no = 0;
  if (textView1 && logFlag) {
    [textView1 setText:[NSString stringWithFormat:@"%04d:%@\n%@", 
                                 no++, status, [textView1 text]]];
  }
}

- (void)appendStatus2:(NSString *)status {
  static int no = 0;
  if (textView2 && logFlag) {
    [textView2 setText:[NSString stringWithFormat:@"%04d:%@\n%@", 
                                 no++, status, [textView2 text]]];
  }
}

@end

